<?php
namespace JsonRpc\Services\Storage;
use JsonRpc\Models\Movement;
use JsonRpc\Models\MovementLine;
use JsonRpc\Models\SysUser;
use JsonRpc\Services\CommonService;
use JsonRpc\Utils\Utils;
use JsonRpc\Services\System\UserService;

/**
 *  用户
 * @author walkor <worker-man@qq.com>
 */
class MovementService
{
    use CommonService;

    private static function getBaseModel(){
        return "JsonRpc\\Models\\Movement";
    }

    public static function ping(){
        return 'OK';
    }

    public function searchList(){
        $list = Movement::get();
        if($list){
            return $list->toArray();
        }
        return [];
    }

    public function goodsList($params){
        $list = MovementLine::where('movement_id',$params['id'])->get();
        if($list){
            return $list->toArray();
        }
        return [];
    }

   static function saveData($params){
        if(isset($params['id']) && $params['id']){
            //更新
            $model = Movement::find($params['id']);

        }else{
            //新增
            unset($params['id']);
            $user = UserService::getInfoById(['id'=>$params['curr_user_id']]);
            $model = new Movement();
            $model->client_id = $user['client_id'];
            $model->org_id = $user['org_id'];
            $model->created_by = $user['id'];
            $model->updated_by = $user['id'];
        }
       $a = time();
       $model->doctype_id = $params['doctype_id'];
       $model->sn = $a;
       $model->movementdate = $params['movementdate'];
       $model->warehouse_id = $params['warehouse_id'];
       $model->description = $params['description'];
       $model->warehouseto_id = $params['warehouseto_id'];
       $model->save();
       $rs = Movement::where('sn',$a)->get();
       return $rs['id'];
    }
}
